//
//  BiMenuListView1.m
//  XinBanEducation
//
//  Created by bindu on 14-6-8.
//  Copyright (c) 2014年 Nahan Cop. All rights reserved.
//

#import "BiMenuListView1.h"
#import "BiPurchaseSupplier.h"
#define K_CELL_HEIGHT   30
#define K_CELL_HEIGHT_IPAD   K_CELL_HEIGHT*2
#define CELL_NAME_LABEL_TAG   0x01

@interface BiMenuListView1()<UITableViewDataSource, UITableViewDelegate>
{
    UITableView * _tableView;
    UIButton * _closeBtn;
    int _tableWidth;
}
@property (nonatomic, retain) NSArray * menuNameArray;
@end

@implementation BiMenuListView1
@synthesize selectIndex;
@synthesize menuNameArray;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        
        self.menuNameArray = [NSArray array];
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        _tableView.clipsToBounds = YES;
        _tableView.layer.cornerRadius = 5;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.layer.borderWidth = 1;
        _tableView.layer.borderColor = UIColorFromRGB(0xcdcdcd).CGColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_tableView];
      
    }
    return self;
}

- (void)dealloc
{
    self.menuNameArray = nil;
    

}

- (void)setNameArray:(NSArray*)nameArray viewX:(int)viewX topY:(int)topY width:(int)width
{
    self.menuNameArray = nameArray;
    
    _tableWidth = width;
    if (nil != self.menuNameArray)
    {
        int cellHeight = K_CELL_HEIGHT;
        
        int controlHeight = self.frame.size.height-topY;
        
        if (self.menuNameArray.count*cellHeight < controlHeight)
        {
            controlHeight = self.menuNameArray.count*cellHeight;
        }
        _tableView.frame = CGRectMake(viewX, topY, width, controlHeight);
        _tableView.scrollEnabled = YES;
    }
    [_tableView reloadData];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self removeFromSuperview];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

        return K_CELL_HEIGHT;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (nil != self.delegate)
    {
        [self.delegate onMenuListView1Select:indexPath.row];
    }
    [self removeFromSuperview];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (nil == self.menuNameArray)
    {
        return 0;
    }
    
    return self.menuNameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"MenuListViewCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 10, 200, 12)];
       
        [nameLabel setFont:[UIFont systemFontOfSize:12]];
        
        nameLabel.textColor = [UIColor blackColor];
        [nameLabel setBackgroundColor:[UIColor clearColor]];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        [nameLabel setTag:CELL_NAME_LABEL_TAG];
        [cell addSubview:nameLabel];
 
        
        UIView *lineImgView = [[UIView alloc] initWithFrame:CGRectMake(0, K_CELL_HEIGHT-1, MAIN_SCREEN_WIDTH, 1)];
        lineImgView.backgroundColor = UIColorFromRGB(0xcdcdcd);
        [cell addSubview:lineImgView];
 
      
    }
    
    UILabel * nameLabel1 = (UILabel *)[cell viewWithTag:CELL_NAME_LABEL_TAG];
    nameLabel1.frame = CGRectMake(0, 12, _tableWidth, 12);

    
    nameLabel1.text = [self.menuNameArray objectAtIndex:indexPath.row];
    

    nameLabel1.textColor = [UIColor blackColor];
    
    
    return cell;

}

@end
